<?php if (!defined('THINK_PATH')) exit();?><div id="access_modal" class="modal hide fade modal-mini">
	<div class="modal-header">
		<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
		<h3>权限</h3>
	</div>
	<div class="modal-body">
		<div id="tree"></div>
	</div>
	<div class="modal-footer">
		<button class="btn" data-dismiss="modal" aria-hidden="true">关闭</button>
		<button class="btn btn-primary" onclick="save_access()">保存</button>
	</div>
</div>

<script type="text/javascript">
$(function(){
	$('#tree').jstree({ 
		core : {
			data : <?php echo ($list); ?>,
			 dblclick_toggle: false
		},
		checkbox : {
			 keep_selected_style : false,
			 three_state: true, // 父级选中自动全选子集
		},
		plugins : [  "checkbox" ]
	});
});

function getChildrenNode(json_data, menu, node){
	var data;
	for(var i=0; i<json_data.length; i++){
		data = json_data[i];
		if(data.children.length == 0){
			if(data.state.selected){
				if(data.icon === true){
					if(menu.indexOf(data.id) == -1){
						menu.push(data.id);
					}
				}else{
					var array = data.id.split('_');
					node.push(array[1]);
					
					if(menu.indexOf(array[0]) == -1){
						menu.push(array[0]);
					}
				}
			}
		}else{
			if(data.state.selected && data.icon === true){
				menu.push(data.id);
			}
			getChildrenNode(data.children, menu, node);
		}
	}
}

function save_access(){
	var menu = [];
	var node = [];
	
	var jstree = $('#tree').data('jstree');
	var json_data = jstree.get_json();
	getChildrenNode(json_data, menu, node);
	
	var node_id = node.join(',');
	var menu_id = menu.join(',');
	
	$.ajax({
		url: '<?php echo __ACTION__; ?>',
		data: {role_id: <?php echo ($role_id); ?>, node_id: node_id, menu_id: menu_id},
		dataType: 'json',
		type: 'post',
		success: function(){
			$('#access_modal').modal('hide');
		}
	});
}
</script>